/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_TFont.h"
#include "frm_control_TString.h"
#include "grf.h"
#include "frm_control_TPanel.h"

using namespace frm;

// TFont //////////////////////////////////////////////////////////////////////

//
void TFont::AddChild(TFont &V)
{
  if (V.parent) { Except("AddChild[V.parent]");}
  V.parent = this; children.Add(&V,false);
}

//
void TFont::CalcAll()
{
  grf::TFont::Name   = Name.GetValue();
  grf::TFont::Height = Height.GetValue() + (Height_Delta.IsNull() ? 0 : Height_Delta.GetValue());
  grf::TFont::Color  = Color.Value;
  grf::TFont::Set(Style.Value);
  grf::TFont::Update();

  CalcChildren(&Name,&Height,&Height_Delta,&Style,&Color);
}

//
void TFont::ToScale(double Scale)
{
  if (scale == Scale) {
    return;
  }
  scale = Scale; 
  grf::TFont::Update(Scale);
}

//
void TFont::Paint(TComponent &Component,TPaint &Paint,char *String)
{
  Paint.DC.SetTextColor(grf::TFont::Color);
  Paint.DC.Select(*this);
  Paint.DC.SetBkMode(grf::TDC::EBkMode_Transparent);
	
  frm::control::TString *str = dynamic_cast<frm::control::TString*>(&Component);
  frm::control::TPanelSelect* ps = str->GetSelectThisOrParent();
  
  if( ps  && (ps->GetEnter() == str) && (ps->selecting ) ){   //|| str->select
	HDC h = Paint.DC.Handle;
	
	grf::TPoint p = Paint.GRect.RThis.LeftTop();
	::TextOut( h, p.X, p.Y, String, ps->sel_min );
	
	p.X += Paint.DC.GetTextExtent( String, ps->sel_min ).Width;
	::SetBkColor( h, grf::TColor(0,0,150) );
	Paint.DC.SetBkMode(grf::TDC::EBkMode_Opaque);
	::TextOut( h, p.X, p.Y, String + ps->sel_min, ps->sel_max - ps->sel_min );
	
	p.X += Paint.DC.GetTextExtent( String + ps->sel_min, ps->sel_max - ps->sel_min ).Width;
	Paint.DC.SetBkMode(grf::TDC::EBkMode_Transparent);
	::TextOut( h, p.X, p.Y, String + ps->sel_max, strlen(String) - ps->sel_max );
  }
  else{
    Paint.DC.TextOut(Paint.GRect.RThis.LeftTop(),String);
  }
}

//
svl::TInfoParam TFont::AddInfoTo(svl::TInfoParam Info_)
{
  svl::TInfoParam Info = svl::TPointer::AddInfoTo(Info_);

  if (!Name.IsNull()) {
    Info.AddAttr("name",svl::TString() << "\"" << Name.GetValue() << "\"");
  }
  if (!Height.IsNull()) {
    Info.AddAttr("height",Height.GetValue());
  }
  svl::TString SStyle; if (!Style.IsNull()) {
    Info.AddAttr("style",Style.GetValue().GetAsString());
  }
  if (!Color.IsNull()) {
    Info.AddAttr("color",svl::TString()
      << Color.GetValue().R << ","
      << Color.GetValue().G << ","
      << Color.GetValue().B);
  }
  return Info;
}

//
void TFont::CalcChildren
(
  svl::TValueString  *PName,
  svl::TValueInteger *PHeight,
  svl::TValueInteger *PHeight_Delta,
  TStyle             *PStyle,
  TColor             *PColor
)
{
  for (int N = 0; N < children.Numb(); N++)
  {
    TFont &Child = *(TFont*)children.Get(N);

    svl::TValueString  *PlName         = PName;
    svl::TValueInteger *PlHeight       = PHeight;
    svl::TValueInteger *PlHeight_Delta = PHeight_Delta;
    TStyle             *PlStyle        = PStyle;
    TColor             *PlColor        = PColor;

    if (!Child.Name  .IsNull())       { PlName   = &Child.Name;}
    if (!Child.Height.IsNull())       { PlHeight = &Child.Height;}
    if (!Child.Height_Delta.IsNull()) { PlHeight_Delta = &Child.Height_Delta;}
    if (!Child.Color .IsNull())       { PlColor  = &Child.Color;}
    if (!Child.Style .IsNull())       { PlStyle  = &Child.Style;}

    Child.grf::TFont::Name   = PlName->GetValue();
    Child.grf::TFont::Height = PlHeight->GetValue() + (PlHeight_Delta->IsNull() ? 0 : PlHeight_Delta->GetValue());
    Child.grf::TFont::Color  = PlColor->Value;
    Child.grf::TFont::Set(PlStyle->Value);
    Child.grf::TFont::Update();

    Child.CalcChildren(PlName,PlHeight,PlHeight_Delta,PlStyle,PlColor);
  }
}

